<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
class Reviews_model extends Model {

    function __construct()
    {
        // Call the Model constructor
        parent::__construct();
    }

   function list_reviews($limit, $start)
    {  // echo "limit".$limit."start".$start;
        try{
            $reviews_sql = "select a.reviews_id,
                    (select lvalue from tbl_lang b where b.lkey=a.title and b.lang='UK' and module='reviews') as title ,
                    a.`status`
                    from tbl_reviews a order by reviews_id, title
                          ";
           $reviews_sql.= " limit ".$start.",".$limit;
            $reviews = $this->db->query($reviews_sql);
            if($reviews->num_rows() > 0) {
                 return $reviews_result = $reviews->result_array();
            } else {
                return FALSE;
            }
        } catch(Exception $e){
            throw new Exception('Error in DB query');
            return FALSE;
        }
    }
  function list_reviews_count()
    {
        try{
            $reviews_sql = "select  count(*) as count
                    from tbl_reviews  
                          ";

            $reviews = $this->db->query($reviews_sql);
            if($reviews->num_rows() > 0) {
                  $reviews_result = $reviews->row_array();
                  return $reviews_result["count"];
            } else {
                return FALSE;
            }
        } catch(Exception $e){
            throw new Exception('Error in DB query');
            return FALSE;
        }
    }

    function savereviews($post_arr, $langarr)
    {
        $this->load->database();
    	$reviewsdetailsarray=array();
        $last_insert_id=-1;
        $reviewsdetailsarray["status"]=$post_arr['status'];
        
        $sql = "INSERT INTO tbl_reviews(status)
                VALUES('" . $reviewsdetailsarray["status"] . "')";
       $query = $this->db->query($sql);
    	if($query)
    	{
            $last_insert_id = $this->db->insert_id();
            $reviews_title_lang_key = $last_insert_id . "_title";            

            $sql = "UPDATE tbl_reviews SET title = '" . $reviews_title_lang_key . "'
                    WHERE reviews_id = " . $last_insert_id;
            $query = $this->db->query($sql);
            if($query)
            {
                foreach ($langarr as $langkey=>$langval)
                {
                    $reviewsdetailsarray['title'.$langkey]=$post_arr['title'.$langkey];
                    $sql = "INSERT INTO tbl_lang(lkey, lang, lvalue, mod_id, module)
                            VALUES('" . $reviews_title_lang_key . "', '" . $langkey . "', '" . $reviewsdetailsarray["title" . $langkey] . "', " . $last_insert_id . ", 'reviews')";
                    $query = $this->db->query($sql);                    
                }
            }
        }
        return $last_insert_id;
           
    }
    
    function get_reviews_details_by_id($reviews_id)
    {
        try {
            $reviews_sql = "SELECT *
                FROM tbl_reviews 
                WHERE reviews_id = ?";
            $reviews = $this->db->query($reviews_sql, array($reviews_id));
            if($reviews->num_rows() > 0) {
                 return $reviews_result = $reviews->result_array();
            } else {
                return FALSE;
            }
        } catch(Exception $e){
            throw new Exception('Error in DB query');
            return FALSE;
        }
    } 
    
    function get_reviews_details_lang_by_id($lkey, $langkey, $module='reviews')
    {
        $sql = "SELECT * FROM tbl_lang WHERE lkey = ? and lang=? and module=?";
        $facilty = $this->db->query($sql, array($lkey, $langkey, $module));
        if($facilty->num_rows() > 0) {
             return $fac_result = $facilty->result_array();
        } else {
            return FALSE;
        }
    }    

    function updatereviews($post_arr, $data)
    {
        //print_r($post_arr['facility']);
        $sql = "UPDATE tbl_reviews
                SET status = '" . $post_arr['status'] . "'
                WHERE reviews_id = " . $post_arr['reviews_id'];
        
        $query = $this->db->query($sql);
        if($query)
        {
            foreach($data['partnerlang'] as $langkey=>$langval)
            {
                $sql_name = "UPDATE tbl_lang SET lvalue = '" . $post_arr['title' . $langkey] . "'
                        WHERE lang='" . $langkey . "'
                            AND mod_id = " . $post_arr['reviews_id'] . "
                                AND lkey = '" . $post_arr['reviews_id'].'_title' . "'
                                    AND module = 'reviews' ";
                
                $query_title = $this->db->query($sql_name);                
            }

            
        }
    }

    function delete_reviews($reviews_id)
    {
        $del_reviews_sql = "DELETE FROM tbl_reviews WHERE reviews_id = " . $reviews_id;
        $reviews_qry = $this->db->query($del_reviews_sql);
        if($reviews_qry)
        {            
            $lang_sql = "DELETE FROM tbl_lang WHERE mod_id = " . $reviews_id . " AND module='reviews' ";
            $lang_qry = $this->db->query($lang_sql);            
        }
    }
    
}
?>
